const state = {
  userId: sessionStorage.getItem('userId') || '',  // 从 sessionStorage 获取用户ID
  nickname: sessionStorage.getItem('nickname') || '',  // 从 sessionStorage 获取昵称
  avatar: sessionStorage.getItem('avatar') || '',  // 从 sessionStorage 获取头像
  url: sessionStorage.getItem('url') || '',  // 从 sessionStorage 获取 URL
}

const mutations = {
  SET_USER_ID(state, userId) {
    state.userId = userId;
    // 同步到 sessionStorage
    const userInfo = JSON.parse(sessionStorage.getItem('userInfo')) || {};
    userInfo.userId = userId;
    sessionStorage.setItem('userInfo', JSON.stringify(userInfo));
    sessionStorage.setItem('userId', userId);  // 单独存储 userId
  },
  SET_NICKNAME(state, nickname) {
    state.nickname = nickname;
    // 同步到 sessionStorage
    const userInfo = JSON.parse(sessionStorage.getItem('userInfo')) || {};
    userInfo.nickname = nickname;
    sessionStorage.setItem('userInfo', JSON.stringify(userInfo));
    sessionStorage.setItem('nickname', nickname);  // 单独存储 nickname
  },
  SET_AVATAR(state, avatar) {
    state.avatar = avatar;
    // 同步到 sessionStorage
    const userInfo = JSON.parse(sessionStorage.getItem('userInfo')) || {};
    userInfo.avatar = avatar;
    sessionStorage.setItem('userInfo', JSON.stringify(userInfo));
    sessionStorage.setItem('avatar', avatar);  // 单独存储 avatar
  },
  SET_URL(state, url) {
    state.url = url;
    // 同步到 sessionStorage
    const userInfo = JSON.parse(sessionStorage.getItem('userInfo')) || {};
    userInfo.url = url;
    sessionStorage.setItem('userInfo', JSON.stringify(userInfo));
    sessionStorage.setItem('url', url);  // 单独存储 url
  },
  SET_USER_INFO(state, { userId, nickname, avatar, url }) {
    state.userId = userId;
    state.nickname = nickname;
    state.avatar = avatar;
    state.url = url;
    // 存储整个用户信息对象到 sessionStorage
    sessionStorage.setItem('userInfo', JSON.stringify({ userId, nickname, avatar, url }));
    sessionStorage.setItem('userId', userId);
    sessionStorage.setItem('nickname', nickname);
    sessionStorage.setItem('avatar', avatar);
    sessionStorage.setItem('url', url);
  }
}

const actions = {
  setUserId({ commit }, userId) {
    commit('SET_USER_ID', userId);
  },
  setNickname({ commit }, nickname) {
    commit('SET_NICKNAME', nickname);
  },
  setAvatar({ commit }, avatar) {
    commit('SET_AVATAR', avatar);
  },
  setUrl({ commit }, url) {
    commit('SET_URL', url);
  },
  setUserInfo({ commit }, userInfo) {
    commit('SET_USER_INFO', userInfo);
  },
  restoreUserInfo({ commit }) {
    const userInfo = sessionStorage.getItem('userInfo');
    if (userInfo) {
      commit('SET_USER_INFO', JSON.parse(userInfo));
    }
  }
}

const getters = {
  userId: (state) => state.userId,
  nickname: (state) => state.nickname,
  avatar: (state) => state.avatar,
  url: (state) => state.url,  // 获取 URL
  userInfo: (state) => ({ userId: state.userId, nickname: state.nickname, avatar: state.avatar, url: state.url }),
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
